package cn.tedu.ivos.application.service.impl;

import cn.tedu.ivos.application.mapper.ApplicationMapper;
import cn.tedu.ivos.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivos.application.pojo.dto.ApplicationSaveParam;
import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos.application.service.ApplicationService;
import cn.tedu.ivos.audit.mapper.AuditMapper;
import cn.tedu.ivos.audit.pojo.vo.AuditVO;
import cn.tedu.ivos.audit.service.AuditService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Transactional
@Service
public class ApplicationServiceImpl implements ApplicationService {
    @Autowired
    private ApplicationMapper mapper;

    @Autowired
    private AuditService auditService;

    @Autowired
    private UserMapper userMapper;
    @Override
    public void save(ApplicationSaveParam applicationSaveParam) {
        Application application = new Application();
        BeanUtils.copyProperties(applicationSaveParam, application);
        application.setCreateTime(new Date());
        application.setStatus(ApplicationStatusEnum.PENDING.getCode());
        // 遇到了问题 新增申请对应的审批数据时，没有拿到对应的申请单id
        // 原因 执行insert对应的sql后，并没有把刚生成的申请单id回填到application对象中
        // 解决办法 给这条mysql上加userGenerateKeys="true" keyProperty="id"属性
        // 效果 JDBC自动回填此申请单id到application对象的id属性中，再传给审批，审批中就有了申请单id
        mapper.save(application);

        // 还许要调用审批层业务，生成1-2条审批单数据
        auditService.insertAudit(application);

    }

    @Override
    public List<ApplicationVO> select(ApplicationQuery query) {
        List<ApplicationVO> list = mapper.select(query);

        for (int i  = 0 ; i < list.size(); i++)
        {
            ApplicationVO applicationVO = list.get(i);
            assignAuditUserList(applicationVO);
        }
        return list;
    }

    private void assignAuditUserList(ApplicationVO applicationVO) {
        // 存储审批人id
        List<Long> auditIdList = new ArrayList<>();
        // 存储审批人姓名
        List<String> auditNameList= new ArrayList<>();
//        查出订单的审批人数据
        List<AuditVO> auditVOList = auditService.selectAuditByApplicationIed(applicationVO.getId());

        for (int i = 0 ; i < auditVOList.size();  i++)
        {
            AuditVO auditVO = auditVOList.get(i);
            Long id = auditVO.getAuditUserId();
            auditIdList.add(id);

            UserVO userVO = userMapper.selectById(id);
            auditNameList.add(userVO.getUsername());

            StringJoiner stringJoiner = new StringJoiner(",");

            for (String name : auditNameList)
            {
                stringJoiner.add(name);
            }

            applicationVO.setAuditUserIdList(auditIdList);
            applicationVO.setAuditUsernameList(stringJoiner.toString());




        }


    }

}
